package at.kaok.betfair;

import java.util.List;
import java.util.logging.Logger;

import betfair.global.APIRequestHeader;
import betfair.global.BFEvent;
import betfair.global.BFGlobalService;
import betfair.global.BFGlobalService_Service;
import betfair.global.GetEventsReq;
import betfair.global.GetEventsResp;
import betfair.global.LoginReq;
import betfair.global.LoginResp;


public class Global {
	private static final Logger log = Logger.getLogger(Global.class.getName());

	
	public static String login(String user, String pwd){
		// TODO Auto-generated method stub
		BFGlobalService_Service sr = new BFGlobalService_Service();
		BFGlobalService service = sr.getPort(BFGlobalService.class);
		
		LoginReq req = new LoginReq();
		req.setIpAddress("0");
		req.setLocationId(0);
		req.setUsername(user);
		req.setPassword(pwd);
		req.setProductId(82);

		LoginResp resp = service.login(req);
		log.warning("LoginResult: "+resp.getErrorCode());
		return resp.getHeader().getSessionToken();							
	}
	
	public static List<BFEvent> getEvents(String session){
		BFGlobalService service =  getService();
		GetEventsReq req = new GetEventsReq();
		req.setHeader(getHeaderWithSession(session));
		req.setEventParentId(1);
		GetEventsResp resp  = service.getEvents(req);
	
		return resp.getEventItems().getBFEvent();		
	}
	
	private static BFGlobalService getService(){
		BFGlobalService_Service sr = new BFGlobalService_Service();
		BFGlobalService it = sr.getPort(BFGlobalService.class);
		return it;
		
	}
	
	private static APIRequestHeader getHeaderWithSession(String session){
		APIRequestHeader header = new APIRequestHeader();
		header.setSessionToken(session);
		return header;
		
	}
}
